﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using FreeSql;
using EAF.Utility;
using EAF.DBC;
using App.Utility;
using App.Model.Main;


namespace App.DTO.Main
{
    /// <summary>
    /// 系统角色 业务字段
    /// </summary>
    public class SysRoleBaseDto
    {
        /// <summary>
        /// 编号
        /// </summary> 
        [Display(Name = "编号")]
        public virtual int id { get; set; }

        /// <summary>
        /// 角色名
        /// </summary>
        [Display(Name = "角色名")]
        [NotNull]
        [StrMaxLen(20)]
        public string name { get; set; }

        /// <summary>
        /// 接口id 1,2,3
        /// </summary>
        [Display(Name = "接口id 1,2,3")]
        public List<int> apiIds { get; set; }

        /// <summary>
        /// 模块id 1,2,3
        /// </summary>
        [Display(Name = "模块id 1,2,3")]
        public List<int> moduleIds { get; set; }

        /// <summary>
        /// 所属平台
        /// </summary>
        [Display(Name = "所属平台")]
        public int platformId { get; set; }

        /// <summary>
        /// 数据版本 业务中无需显示维护值 用于乐观锁添加传0 更新传详情接口的值
        /// </summary>
        [Display(Name = "数据版本")]
        public virtual int version { get; set; }

        #region 权限编辑用到的字段

        /// <summary>
        /// 当前role所有模块权限id  权限编辑
        /// </summary>
        public virtual List<string> currModuleApi { get; set; }
        /// <summary>
        /// 选中的树节点  权限编辑
        /// </summary>
        public virtual List<string> selectedId { get; set; }


        #endregion
    }

    /// <summary>
    /// 系统角色 分页请求json实体类 这里字段请使用首字母小写
    /// </summary>
    public class SysRolePageReq : DtoPageReqBase
    {
        /// <summary>
        /// 常用字段搜索 默认根据支持CreaterName模糊查询
        /// </summary>
        [Display(Name = "常用字段搜索")]
        public string searchValue { get; set; }

        /// <summary>
        /// 查询参数处理
        /// </summary>
        /// <param name="dbc">表所在库上下文</param>
        /// <returns></returns>
        public ISelect<SysRole> Query(BaseDbContext dbc)
        {
            var query = dbc.Select<SysRole>();

            //显示模式处理
            if (showMode != ShowMode.Normal)
            {
                query.DisableGlobalFilter();
                if (showMode == ShowMode.Deleted)
                {
                    query = query.Where(w => w.Deleted == true);
                }
            }

            //前端不传入whereDynamicFilter高级过滤 特殊条件过滤
            if (whereDynamicFilter.IsNull())
            {
                //这里处理常用字段查询处理
                query = query.WhereIf(searchValue.HasValue(), w => w.CreaterName.Contains(searchValue));
            }
            else
            {
                //高级过滤查询
                var whereObj = dbc.ParseWhereDynamicFilter(whereDynamicFilter);
                if (whereObj != null)
                {
                    query = query.WhereDynamicFilter(whereObj);
                }
            }
            //前端不传排序字符串 则按id降序
            if (orderBy == null)
            {
                query = query.OrderByDescending(f => f.Id);
            }
            else
            {
                query = query.OrderBy(orderBy);
            }

            return query;
        }

    }

    /// <summary>
    /// 系统角色 请求json实体类
    /// </summary>
    public class SysRoleReq : SysRoleBaseDto
    {
        /// <summary>
        /// json实例转换为db实例 就是将同名字段赋值到dbEntity 相当于更新
        /// </summary>
        /// <param name="dbEntity">表实例</param>
        /// <returns></returns>
        public SysRole ConvertDbEntity(SysRole dbEntity)
        {
            this.AutoMapObjectTo(dbEntity);

            //无法自动绑定的在此手动绑定


            return dbEntity;
        }

        #region 通用字段

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public virtual string description { get; set; }

        /// <summary>
        /// 排序码
        /// </summary>
        [Display(Name = "排序码")]
        public virtual int? sortNo { get; set; }

        /// <summary>
        /// 是否有效
        /// </summary>
        [Display(Name = "是否有效")]
        public virtual bool valid { get; set; }

        #endregion

    }

    /// <summary>
    /// 系统角色 响应json实体类
    /// </summary>
    public class SysRoleResp : SysRoleBaseDto
    {
        public SysRoleResp() { }

        /// <summary>
        /// db实例转换json实例 就是将同名字段赋值到当前实例 相当于更新复制
        /// </summary>
        /// <param name="dbEntity">db实例</param>
        public SysRoleResp(SysRole dbEntity)
        {
            dbEntity.AutoMapObjectTo(this);

            //无法自动绑定的在此手动绑定

        }

        #region 通用字段

        /// <summary>
        /// 描述
        /// </summary>
        [Display(Name = "描述")]
        public virtual string description { get; set; }

        /// <summary>
        /// 排序数字
        /// </summary>
        [Display(Name = "排序数字")]
        public virtual int? sortNo { get; set; }

        /// <summary>
        /// 是否有效
        /// </summary>
        [Display(Name = "是否有效")]
        public virtual bool valid { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        [Display(Name = "创建者ID")]
        public virtual int? createrId { get; set; }

        /// <summary>
        /// 创建人名
        /// </summary>
        [Display(Name = "创建人名")]
        public virtual string createrName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Name = "创建时间")]
        public virtual DateTime createTime { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>
        [Display(Name = "修改时间")]
        public virtual DateTime? updateTime { get; set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        [Display(Name = "是否删除")]
        public virtual bool deleted { get; set; }

        /// <summary>
        /// 删除时间
        /// </summary>
        [Display(Name = "删除时间")]
        public virtual DateTime? deleteTime { get; set; }

        #endregion

    }

    /// <summary>
    /// 所有模块权限树
    /// </summary> 
    public class AllModuleApiTreeResp
    {

        public string id { get; set; }

        public int sourceId { get; set; }

        public string label { get; set; }

        public List<AllModuleApiTreeResp> children { get; set; }
    }

}


